package org.springframework.tx.test;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.sql.DataSource;

/**
 * [简要描述]:
 * [详细描述]:
 *
 * @author ZouHaiBo
 * @version 1.0, 2021/8/11 16:25
 * @since JDK 1.8
 */
@Configuration
public class DataSourceConfig
{


	@Bean
	public DataSource dataSource() {

		HikariDataSource dataSource = new HikariDataSource();

		dataSource.setJdbcUrl("jdbc:mysql://192.168.206.201:3306/content_center_test");
		dataSource.setPassword("Content123");
		dataSource.setUsername("contentuser");
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");

		return dataSource;
	}

	@Bean
	@Autowired
	public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {

		return new DataSourceTransactionManager(dataSource);
	}


	@Bean
	@Autowired
	public JdbcTemplate jdbcTemplate(DataSource dataSource) {

		return new JdbcTemplate(dataSource);
	}
	@Bean
	public TransactionTemplate transactionTemplate(DataSourceTransactionManager dataSourceTransactionManager) {
		return new TransactionTemplate(dataSourceTransactionManager);
	}

}
